#!/bin/bash

:<<@

Author:庞林
Created Time:2022-11-9
Script Description:登录注册之类的

@

# 添加帮助信息
help_fun(){
	echo '
user.sh 动作       进入交互模式
	add        添加用户【账号两字以上，密码6字以上，均不可包含空格】
	del        删除用户
	log        登录用户【默认】
	--help     显示帮助信息
'

}


# 添加用户
add(){
	while true
	do
		read -p '请输入您要创建的用户名' UserName
		
		alone=0	
		if [ -f ./UserList.conf ] ;then
                        cat ./UserList.conf | awk '{print $1}' | grep "$UserName" > /dev/null 
			[ $? -ne 0 ]  || alone=1
			[ $alone -eq 1 ] && echo '你的账号与其他账号冲突了'
		else 
			alone=1
                fi
	
		NameLen=${#UserName}
		echo $UserName | grep ' ' > /dev/null
                if [ $? -ne 0 ] && [ $NameLen -gt 2 ] && [ $alone -eq 0 ] ; then
                        echo '账号正确正确'
                        break
                fi
	done

	while true
	do
		read -p "请输入您的用户的密码" UserPwd
		PwdLen=${#UserPwd}
		echo $UserPwd | grep ' ' > /dev/null
		if [ $? -ne 0 ] && [ $PwdLen -gt 5 ] ; then
			echo '密码正确'
			break
		fi	
	done
	echo "$UserName $UserPwd" >> ./UserList.conf

}



login(){
	echo '==============================================='
	echo '-----------欢迎登录傻狗的小屋------------------'	
	while true
	do
		read -p '请输入您的账号' UserName
		
		if [ $UserName == 'root' ]  ; then
			echo '警告！您正在进入root用户'
			RootStatus=1
			break
		fi
		
		echo $LockUser | grep -w "$UserName"
		[ $? -eq 0 ] && echo '您的用户锁定了'  &&continue

		if [ -f ./UserList.conf ] ;then
			cat ./UserList.conf | awk '{print $1}' | awk "/$UserName/" &> /dev/null
			if [ $? -eq 0 ] ; then
				break
			fi
		else
			echo '没有您的账号'
		fi
	done
	
        UserLine=$(grep -w "$UserName" ./UserList.conf)
	ChackPwd=$(echo  $UserLine | awk '{print $2}')

	for i in {1..3}
	do
		[ $RootStatus -eq 1 ] && num=1 || num=3
		echo "您一共有${num}次机会"
		read -p "请输入第${i}次密码" UserPwd

		
		if [ $RootStatus -eq 1 ] ; then
			if [ $UserPwd != '1234567890' ] ; then
				echo 'root登录失败'
				exit
			fi 
			echo 'root登录成功'	
			LogUser='root'			
			break
			
		fi
		
		
		if [ $ChackPwd != $UserPwd ] && [ $i -eq 3 ] ; then
			echo "用户${UserName}已被锁定"
			LockUser="${LockUser} ${UserName}"
			break
		elif [ $ChackPwd != $UserPwd ]; then
			echo '密码错误'
			continue;
		fi
		
		
		echo '密码成功'
		LogUser="$UserName"
		break
	done	
	
	echo $UserName > ./LockUser.conf
	
	echo '接下来你想干啥

del、删除
add、添加
help、帮助
'	
	read action
	if [ $action == 'add' ] ; then
		main 'add'
	elif [ $action == 'help' ] ; then

		main '--help'
	elif [ $action == 'del' ] ; then
		main 'del'
	fi
}


del(){
	echo '删除用户'
	if [ $LogUser != 'root' ] ; then
		echo '抱歉您的权限不足'
		return 1
	fi
	read -p '请输入您要删除用户' UserName 
	UserRow=$(awk '{print NR,$1}' ./UserList.conf | grep -w "$UserName")
	if [ $? -eq 0 ] ; then
		UserIndex=$(echo $UserRow | awk '{print $1}')
		sed -ir "${UserIndex}s#.*\n##" ./UserList.conf
		sed -r 's#^$##' ./UserList.conf
		echo '用户删除成功'
	else
		echo '用户删除失败'
	fi
}



# 用户状态
LogUser=''

# 锁定用户
[ -f ./LockUser.conf ] && LockUser=$(cat ./LockUser.conf) || LockUser=''


main(){

	if [[ $1 =~ ^$ ]] || [ $1 == 'login' ] ; then
		login

	elif [ $1 == 'del' ] ; then
		del

	elif [ $1 == 'add' ] ; then
		add

	elif [ $1 == '--help' ] ; then
		help_fun
	else

		echo '您的动作输入错误'
		help_fun	

	fi

}



main $1
